﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using SSOCommon;
using SSOServer.Config;
using System.Xml.Serialization;

namespace SSOServer
{
    public static class CoreServices
    {
        public static string GenerateToken()
        {
            return DateTime.Now.Ticks.ToString();
        }


        public static void Login(HttpContext current)
        {
            //set response header
            current.Response.Clear();
            current.Response.ContentType = "text/html";
            string token = Tools.GetParamValue(ArgumentConstant.TOKEN);
            Dictionary<string, object> args = new Dictionary<string, object>();
            args.Add(ArgumentConstant.SENDER, SenderSource.Server);
            if (token == String.Empty)
            {
                current.Response.Write(Tools.GenerateUrl(SSOConfig.Instance.GlobalSettings.SingleLoginUrl, args));
                current.Response.End();
                return;
            }
            else
            {
                if (StateManager.IsExist(token))
                {
                    UserInfo userInstance = StateManager.GetUserInfo(token);
                    if (userInstance != null)
                    {
                        string redirectUrl = Tools.GetParamValue(ArgumentConstant.REDIRECTURL);
                        if (String.IsNullOrEmpty(redirectUrl))
                        {
                            string siteId = Tools.GetParamValue(ArgumentConstant.SITEID);
                            SiteElement site = SSOConfig.Instance.GetSite(siteId);
                            redirectUrl = site.DefaultUrl;
                        }
                        args.Add(ArgumentConstant.TOKEN, token);
                        current.Response.Write(Tools.GenerateUrl(redirectUrl, args));
                        current.Response.End();
                        return;
                    }
                }
            }
            current.Response.End();
        }

        public static void Logout(HttpContext current)
        {
            //set response header
            current.Response.Clear();
            current.Response.ContentType = "text/html";
            string token = Tools.GetParamValue(ArgumentConstant.TOKEN);
            if (StateManager.IsExist(token))
            {
                StateManager.Remove(token);
            }
            current.Response.End();
        }

        public static void Auth(HttpContext current)
        {
            //set response header
            current.Response.Clear();
            current.Response.ContentType = "text/plain";
            string token = Tools.GetParamValue(ArgumentConstant.TOKEN);
            if (StateManager.IsExist(token))
            {
                current.Response.Write("true");
            }
            else
            {
                current.Response.Write("false");
            }
            current.Response.Flush();
            current.Response.End();
        }

        public static void GetUserInfo(HttpContext current)
        {
            //set response header
            current.Response.Clear();
            current.Response.ContentType = "text/html";
            string token = Tools.GetParamValue(ArgumentConstant.TOKEN);
            UserInfo user = StateManager.GetUserInfo(token);
            current.Response.Write(Tools.SerializeData<UserInfo>(user));
            current.Response.Flush();
            current.Response.End();
        }
    }
}